package com.yishu.Dao;

import com.yishu.entity.Book;
import com.yishu.util.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class BookDao {

    DBConnection dbConnection = new DBConnection();

    /**
     * 展示所有图书列表
     *
     * @return
     * @throws Exception
     */
    public List<Book> showAllBook() throws Exception {
        List<Book> bookList = new ArrayList<>();
        String sql = "select * from t_books";
        Connection connection = dbConnection.getCon();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            Book book = new Book();
            book.setBookId(resultSet.getInt("bookId"));
            book.setBookName(resultSet.getString("bookName"));
            book.setBookDescribe(resultSet.getString("bookDescribe"));
            book.setBookImage(resultSet.getString("bookImage"));
            book.setBookNumbers(resultSet.getInt("bookNumbers"));
            book.setBookPrice(resultSet.getInt("bookPrice"));
            bookList.add(book);
        }

        return bookList;
    }

    /**
     * 查询一本书的具体的信息
     *
     * @param bookId
     * @return
     * @throws Exception
     */
    public Book showABookDetail(int bookId) throws Exception {
        Connection connection = dbConnection.getCon();
        String sql = "select * from t_books where bookId = '" + bookId + "'";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        Book book = new Book();
        if (resultSet.next()) {
            book.setBookId(bookId);
            book.setBookName(resultSet.getString("bookName"));
            book.setBookNumbers(resultSet.getInt("bookNumbers"));
            book.setBookImage(resultSet.getString("bookImage"));
            book.setBookPrice(resultSet.getInt("bookPrice"));
            book.setBookDescribe(resultSet.getString("bookDescribe"));
        }

        return book;
    }

    /**
     * 关键字搜索书籍
     *
     * @return
     * @throws Exception
     */
    public List<Book> showBookByKeyWords(String keyWord) throws Exception {
        String sql = "select * from t_books where bookName like '%" + keyWord + "%'";
        Connection connection = dbConnection.getCon();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<Book> bookList = new ArrayList<>();
        while (resultSet.next()) {
            Book book = new Book();
            book.setBookId(resultSet.getInt("bookId"));
            book.setBookName(resultSet.getString("bookName"));
            book.setBookDescribe(resultSet.getString("bookDescribe"));
            book.setBookImage(resultSet.getString("bookImage"));
            book.setBookNumbers(resultSet.getInt("bookNumbers"));
            book.setBookPrice(resultSet.getInt("bookPrice"));
            bookList.add(book);
        }


        return bookList;
    }
}
